from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fastapi import FastAPI
from src.common.logger import get_logger
from src.crawler.maersk.maersk_crawler import maersk_spider_run
from src.crawler.cosco.cosco_crawler import cosco_spider_run
from src.crawler.emc.EMC_crwaler import emc_spider_run
from src.crawler.hapag.hapag_crawler import hapag_spider_run
from src.crawler.maston.matson_crawler import matson_spider_run
from src.crawler.one.one_crawler import zh_hans_spider_run
from src.crawler.oocl.oocl_crawler import oocl_spider_run
from src.crawler.yml.yml_crawler import yangming_spider_run
from src.crawler.zim.zim_crawler import zim_spider_run
##以下爬虫需要开启vpn运行
from src.crawler.hmm.hmm_crawler import hmm21_spider_run
from src.crawler.cma.cma_crawler import cma_spider_run  ##需要国外ip代理池

logger = get_logger(__name__)
scheduler = AsyncIOScheduler()

def register_scheduler(app: FastAPI):
    @app.on_event("shutdown")
    async def shutdown():
        scheduler.shutdown()

    @app.on_event("startup")
    async def startup():
        scheduler.add_job(maersk_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(cosco_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(emc_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(hapag_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(matson_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(zh_hans_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        # scheduler.add_job(oocl_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(zim_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(yangming_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(cma_spider_run, 'cron', day_of_week='wed', hour=10, minute=0)
        scheduler.start()

def vpn_register_scheduler(app: FastAPI):
    @app.on_event("shutdown")
    async def shutdown():
        scheduler.shutdown()

    @app.on_event("startup")
    async def startup():
        scheduler.add_job(hmm21_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.add_job(cma_spider_run, 'cron', day_of_week='wed', hour=0, minute=0)
        scheduler.start()
